package cn.tedu.bonfire.front.content.controller;

import cn.tedu.bonfire.common.enumerator.ServiceCode;
import cn.tedu.bonfire.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.bonfire.common.pojo.vo.PageData;
import cn.tedu.bonfire.common.web.JsonResult;
import cn.tedu.bonfire.front.content.pojo.dto.ArticleAddNewDto;
import cn.tedu.bonfire.front.content.pojo.dto.UpdateArticleRequestDto;
import cn.tedu.bonfire.front.content.pojo.entity.Article;
import cn.tedu.bonfire.front.content.pojo.vo.*;

import cn.tedu.bonfire.front.content.service.IArticleService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/article")
@Validated
@Slf4j
@Api(tags = "1. 文章管理")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    @GetMapping("/views/{userId}")
    @ApiOperation("根据id查询用户文章的浏览数")
    @ApiOperationSupport(order = 600)
    public JsonResult selectUserViews (@PathVariable Long userId) {
        Long views = articleService.selectUserViews(userId);
        return JsonResult.ok(views);
    }

    @GetMapping("/articles/{userId}")
    @ApiOperation("根据id查询用户发布的文章数")
    @ApiOperationSupport(order = 610)
    public JsonResult selectUserArticles (@PathVariable Long userId) {
        Long articles = articleService.selectUserArticles(userId);
        return JsonResult.ok(articles);
    }


    @GetMapping("/collect/{userId}")
    @ApiOperation("根据id查询用户收藏的文章数")
    @ApiOperationSupport(order = 630)
    public JsonResult selectUserCollect (@PathVariable Long userId) {
        Long Collect = articleService.selectUserCollect(userId);
        return JsonResult.ok(Collect);
    }


    @GetMapping("/likes/{userId}")
    @ApiOperation("查询点赞数量")
    @ApiOperationSupport(order = 620)
    public JsonResult selectUserLikes(@PathVariable Long userId) {
        Long likes = articleService.selectUserLikes(userId);
        return JsonResult.ok(likes);
    }
    @GetMapping ("/view")
    @ApiOperation("文章浏览量")
    @ApiOperationSupport(order = 431)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章id", paramType = "query", dataType = "long"),
    })
    public JsonResult updateView(Long id){
        articleService.updateViewById(id);
        return JsonResult.ok();
    }
    @PostMapping("/add-new")
    @ApiOperation("发布文章")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                             @ApiIgnore HttpServletRequest request,@Valid ArticleAddNewDto articleAddNewDto) {
        log.debug("开始处理【发布文章】的请求，参数：{}", articleAddNewDto);
        String remoteAddr = request.getRemoteAddr();
        articleService.addNew(currentPrincipal, remoteAddr, articleAddNewDto);
        return JsonResult.ok();
    }


    /**
     * 笔记管理页面--根据id修改笔记状态(公开或私密)
     *
     * @param currentPrincipal     当事人信息
     * @param updateArticleRequestDto 修改状态的参数
     * @return
     */
    @PostMapping("/updateArticle")
    @ApiOperation("根据笔记id修改笔记审核状态(公开或私密)")
    @ApiOperationSupport(order = 311)

    public JsonResult updateArticle(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                 @Validated UpdateArticleRequestDto updateArticleRequestDto) {
        log.debug("开始处理【笔记管理页面--根据id修改笔记审核状态】的请求，当事人:{},参数{}", currentPrincipal, updateArticleRequestDto);
        articleService.updateArticle(currentPrincipal, updateArticleRequestDto);
        return JsonResult.ok();
    }



    @PostMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询文章")
    @ApiOperationSupport(order = 200)
    public JsonResult getStandardById( @PathVariable @Range(min = 1, message = "请提交有效的文章ID值！") Long id){
        ArticleStandardVO queryResult = articleService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }
    @GetMapping("/recommend")
    @ApiOperation("查询推荐的文章列表")
    @ApiOperationSupport(order = 420)
    public JsonResult listByRecommend() {
        PageData<ArticleListItemVO> pageData = articleService.listByRecommend();
        return JsonResult.ok(pageData.getList());
    }


    @GetMapping("/{categoryId}/{isRecommend}")
    @ApiOperation("根据类别查询文章列表")
    @ApiOperationSupport(order = 421)
    public JsonResult listByCategoryAndRecommend(@PathVariable @Range(message = "请提交有效的文章类别ID值！") Long categoryId,
                                     @Range(min = 1, message = "请提交有效的推荐值！") Integer isRecommend) {
        PageData<ArticleListItemVO> pageData = articleService.listByCategory(categoryId, isRecommend);
        return JsonResult.ok(pageData);
    }
    @GetMapping("/selectArticle/{userId}")
    @ApiOperation("查询用户发布的文章(审核通过的)")
    @ApiOperationSupport(order = 422)
    public JsonResult selectMyArticle(@PathVariable Long userId) {
        List<UserCollectListVO> collectList = articleService.selectMyArticle(userId);
        return JsonResult.ok(collectList);
    }
    @GetMapping("/selectMyAuditArticle/{userId}")
    @ApiOperation("查询用户发布的文章(待审核的)")
    @ApiOperationSupport(order = 423)
    public JsonResult selectMyAuditArticle(@PathVariable Long userId) {
        List<UserCollectListVO> collectList = articleService.selectMyAuditArticle(userId);
        return JsonResult.ok(collectList);
    }

    @GetMapping("/{categoryId}")
    @ApiOperation("根据类别查询文章列表")
    @ApiOperationSupport(order = 422)
    public JsonResult listByCategory(@PathVariable @Range(message = "请提交有效的文章类别ID值！") Long categoryId,
                                     @Range(min = 1, message = "请提交有效的数据条数！") Integer num) {
        Integer pageSize = num == null ? 10 : num;
        PageData<ArticleListItemVO> pageData = articleService.listByCategory(categoryId,1,pageSize);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/selectLike")
    @ApiOperation("查询用户点赞文章")
    @ApiOperationSupport(order = 440)
    public JsonResult selectLike(@AuthenticationPrincipal CurrentPrincipal currentPrincipal) {
        List<UserCollectListVO> collectList = articleService.selectLike(currentPrincipal.getId());
        return JsonResult.ok(collectList);
    }

    @GetMapping("/selectIsRecommend")
    @ApiOperation("查询用户点赞文章排序")
    @ApiOperationSupport(order = 442)
    public JsonResult getIsRecommend() {
        List<UserCollectListVO> isRecommend = articleService.getIsrecommend();
        return JsonResult.ok(isRecommend);
    }

    @GetMapping("/selectCollect/{authorId}")
    @ApiOperation("查询用户收藏文章")
    @ApiOperationSupport(order = 441)
    public JsonResult selectCollect(@PathVariable Long authorId) {
        List<UserCollectListVO> collectList = articleService.selectCollect(authorId);
        return JsonResult.ok(collectList);

    }
    @GetMapping("/selectdraft/{userId}/{pageNum}")
    @ApiOperation("查询用户草稿")
    @ApiOperationSupport(order = 441)
    public JsonResult selectDraft(@PathVariable Long userId,@PathVariable String pageNum) {
        Integer Num= Integer.parseInt(pageNum);
        PageData<UserDraftListVO> pageData = articleService.selectdraft(userId, Num,10);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/selectDraftDetail/{authorId}/{articleId}")
    @ApiOperation("查询用户草稿详情")
    @ApiOperationSupport(order = 443)
    public JsonResult selectDraftDetail(@PathVariable Long authorId,@PathVariable Long articleId) {
        UserDraftListVO list = articleService.selectDraftDetail(authorId,articleId);
        return JsonResult.ok(list);
    }




    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除文章")
    @ApiOperationSupport(order = 500)
    public JsonResult deleteById( @PathVariable @Range(min = 1, message = "请提交有效的文章ID值！") Long id){
        articleService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/image")
    @ApiOperation("根据文章id查询用户信息")
    @ApiOperationSupport(order=430)
    public JsonResult getAuthorByArticleId(@PathVariable @Range(min = 1, message = "请提交有效的文章ID值！")Long id){
        ArticleAuthorVO articleAuthorVO = articleService.getAuthorByArticleId(id);
        return JsonResult.ok(articleAuthorVO);
    }

}
